//
//  HZWebViewController.m
//  HZTC_YaDeArt
//
//  Created by MayerF on 17/4/10.
//  Copyright © 2017年 HZTC. All rights reserved.
//

#import "HZWebViewController.h"
#import "HZWebView.h"
#import "HZSearchViewController.h"
#import "HZSingleStockViewController.h"

@interface HZWebViewController ()
@property (nonatomic, strong)HZWebView *pWebView;
@property (nonatomic, copy)NSString *pTitle;
@property (nonatomic, copy)NSString *pUrl;
@end

@implementation HZWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //关联逻辑管理对象
    self.logicManager = [HZBaseLogjcManager new];
    self.view.logicManager = self.logicManager;
    
    _pWebView = [HZWebView new];
    [self.view addSubview:_pWebView];
    [self p_initConstraints];
    
    NSDictionary *dict = self.params;
    if ([dict.allKeys containsObject:@"title"]) {
        _pTitle = dict[@"title"];
    }
    if ([dict.allKeys containsObject:@"url"]) {
        _pUrl = dict[@"url"];
    }
    
    self.navigationItem.title = _pTitle;
    [self p_loadWeb];
    
    kWeakSelf
    _pWebView.requestBlock = ^{
        if ([weakSelf.pWebView canGoBack]) {
            [weakSelf addLeftNavBar];
        }
    };
    
    _pWebView.changeNavTitleBlock = ^(id obj) {
        weakSelf.navigationItem.title = obj;
    };
    
    // "name": "平安银行", "code": "000001", "type": 2, "market": 2}
    _pWebView.jumpStockBlock = ^(NSDictionary *obj) {
        NSString *code = @"";
        NSString *name = @"";
        int type = 2;
        int market = 2;
        
        if ([obj.allKeys containsObject:@"name"]) {
            name = obj[@"name"];
        }
        if ([obj.allKeys containsObject:@"code"]) {
            code = obj[@"code"];
        }
        if ([obj.allKeys containsObject:@"type"]) {
            type = [obj[@"type"] intValue];
        }
        if ([obj.allKeys containsObject:@"market"]) {
            market = [obj[@"market"] intValue];
        }
        
        HZOwnedStockModel *model = [HZOwnedStockModel new];
        model.model_Name = name;
        model.model_Code = code;
        model.nModel_Style = type;
        model.nModel_Market = market;
        UIViewController *nextVC = [[HZSingleStockViewController alloc] initWithCodeInfo:model];
        [weakSelf.navigationController pushViewController:nextVC animated:true];
    };
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    NSArray *stackList = self.navigationController.viewControllers;
    if (stackList.count > 1) {
        [self addLeftNavBar];
    }
}
#pragma mark -
#pragma mark private
- (void)p_initConstraints
{
    [_pWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
}
- (void)p_loadWeb
{
    if (_pUrl && ![_pUrl isEqualToString:@""]) {
        NSMutableURLRequest *requestM = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:_pUrl]];
        NSString *session = [HZUserManager manager].session;
        session = session == nil? @"": session;
        [requestM setValue:session forHTTPHeaderField:@"session"];
        [_pWebView loadRequest:requestM];
    }
}

#pragma mark -
#pragma mark blockAction

- (void)navBackAction
{
    NSArray *stackList = self.navigationController.viewControllers;
    if (stackList.count <= 1) {
        NSUInteger count = _pWebView.backForwardList.backList.count;
        if ([_pWebView canGoBack]) {
            [_pWebView goBack];
            count--;
            if (count <= 0) {
                [self clearNavBar];
            }
        }
    } else {
        if ([_pWebView canGoBack]) {
            [_pWebView goBack];
        } else {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}

- (void)navCloseAction
{
    NSArray *stackList = self.navigationController.viewControllers;
    if (stackList.count <= 1) {
        if ([_pWebView canGoBack]) {
            [_pWebView goToBackForwardListItem:_pWebView.backForwardList.backList.firstObject];
            [self clearNavBar];
        }
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}


- (void)addLeftNavBar
{
    if (self.navigationItem.leftBarButtonItems) { return;}
    
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    backBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
    backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [backBtn setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(navBackAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    closeBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
    closeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [closeBtn setImage:[UIImage imageNamed:@"nav_close2"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(navCloseAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *closeItem = [[UIBarButtonItem alloc] initWithCustomView:closeBtn];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItems = @[backItem, closeItem];
}

- (void)clearNavBar
{
    if (self.navigationItem.leftBarButtonItems) {
        self.navigationItem.leftBarButtonItems = nil;
    }
}

@end
